﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Security;
using TaDMS.Commons;
using TaDMS.Models;
using TaDMS.Filters;

namespace TaDMS.Controllers
{
    [Authorize]
    public class ApiLoginController : ApiController
    {
        private readonly TaDMSMembershipProvider _membershipProvider = new TaDMSMembershipProvider();

        [HttpPost]
        [AllowAnonymous]
        public HttpResponseMessage Logon(Account entity)
        {
            if (ModelState.IsValid)
            {
                if (_membershipProvider.ValidateUser(entity.Username, entity.Password))
                {
                    FormsAuthentication.SetAuthCookie(entity.Username, true);

                    var success = new
                    {
                        success = true,
                        login = true,
                        message = "Login success"
                    };
                    return Request.CreateResponse(HttpStatusCode.OK, success);
                }
                else
                {
                    var loginFalse = new
                        {
                            success = true,
                            login = false,
                            message = "Login failse"
                        };
                    return Request.CreateResponse(HttpStatusCode.OK, loginFalse);
                }
            }
            var error = new
                {
                    success = false,
                    message = "Don't get current UserId"
                };
            return Request.CreateResponse(HttpStatusCode.OK, error);
        }

        [HttpPost]
        [AllowAnonymous]
        public HttpResponseMessage LogOff()
        {
            try
            {
                FormsAuthentication.SignOut();

                var success = new
                {
                    success = true,
                    message = "Logout success"
                };
                return Request.CreateResponse(HttpStatusCode.OK, success);
            }
            catch (Exception e)
            {
                var error = new
                {
                    success = false,
                    message = e.Message
                };
                return Request.CreateResponse(HttpStatusCode.OK, error);
            }
        }

        [HttpGet]
        public HttpResponseMessage GetUserInfo()
        {
            try
            {
                using (var db = new UnitOfWork().AccountRepository)
                {
                    int? total;
                    var currentUsername = User.Identity.Name;
                    var currentUser = db.GetData(out total,
                                                   orderBy: order => order.OrderBy(v => v.Username),
                                                   filter: v => v.Username.Equals(currentUsername),
                                                   includeProperties: "Employee");

                    var enumerable = currentUser.ToList();
                    if (String.IsNullOrEmpty(db.ErrorMessage))
                    {
                        if (enumerable[0] != null)
                        {
                            var result = new
                            {
                                data = enumerable[0],
                                success = true,
                                message = "success",
                            };
                            return Request.CreateResponse(HttpStatusCode.OK, result);
                        }

                        var empty = new
                        {
                            success = true,
                            message = "Employees is empty"
                        };
                        return Request.CreateResponse(HttpStatusCode.OK, empty);
                    }
                    
                    var error = new
                    {
                        success = false,
                        message = db.ErrorMessage
                    };
                    return Request.CreateResponse(HttpStatusCode.OK, error);
                   
                }
            }
            catch (Exception e)
            {
                var error = new
                {
                    success = false,
                    message = e.Message
                };
                return Request.CreateResponse(HttpStatusCode.OK, error);
            }
        }
    }
}
